﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 桌面相关;

public class OrganizeDesktopCsharp
{
    public static void OrganizeDesktop()
    {
        // 获取桌面路径
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        // 创建分类目录
        string picDir = Path.Combine(desktopPath, "Images");
        string docDir = Path.Combine(desktopPath, "Documents");
        string musicDir = Path.Combine(desktopPath, "Music");
        string videoDir = Path.Combine(desktopPath, "Videos");

        Directory.CreateDirectory(picDir);
        Directory.CreateDirectory(docDir);
        Directory.CreateDirectory(musicDir);
        Directory.CreateDirectory(videoDir);

        // 枚举桌面文件
        foreach (string filePath in Directory.GetFiles(desktopPath))
        {
            if (Path.GetFileName(filePath).StartsWith(".")) continue;

            string ext = Path.GetExtension(filePath).ToLower();
            string destDir = null;

            // 根据扩展名分类
            switch (ext)
            {
                case ".jpg":
                case ".jpeg":
                case ".png":
                case ".bmp":
                case ".gif":
                    destDir = picDir;
                    break;
                case ".doc":
                case ".docx":
                case ".pdf":
                case ".txt":
                case ".xls":
                case ".xlsx":
                    destDir = docDir;
                    break;
                case ".mp3":
                case ".wav":
                case ".flac":
                    destDir = musicDir;
                    break;
                case ".mp4":
                case ".avi":
                case ".mkv":
                case ".mov":
                    destDir = videoDir;
                    break;
                default:
                    continue;
            }

            // 移动文件
            string destPath = Path.Combine(destDir, Path.GetFileName(filePath));
            try
            {
                File.Move(filePath, destPath);
                Console.WriteLine($"Moved: {Path.GetFileName(filePath)} to {destDir}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to move {Path.GetFileName(filePath)}: {ex.Message}");
            }
        }
    }
}
